package com.manger.format;

import com.manger.common.office.sax.ExcelFactory;
import com.manger.model.GoodPay;
import com.manger.model.JDD;
import com.manger.model.Project;
import com.manger.utils.*;
import org.apache.poi.ss.formula.functions.Na;

import java.io.File;
import java.io.FileOutputStream;
import java.util.*;

/**
 *@ClassName OrangeMoreMoreCaseFilesFormat
 *@Description  TODO
 *@Author @LiuHongJun
 *@Date 2025/9/1 16:01
 *@Version 1.0
 */
public class OrangeMoreMoreCaseFilesFormat implements CaseFilesFormat{

    private static Map<String, LoanFileEnum> filesMapping = new HashMap<>();

    private static String ocrIdCardMark = "被告身份证";

    private Map<String, String> idCardMap = new HashMap<>();

    @Override
    public void format(String extractDir, String targetDir, Project project) throws Exception {
        System.out.println("++++++++++++++++++++++桔多多案件数据整理+++++++++++++++++++++++++++++");
        List<String> ignoreFileList = project.getIgnoreFilesList();
        List<String> ignoreDirList = project.getIgnoreDirList();
        String excelFilePath = extractDir + "/" + SystemConstant.ENTRUSTED_INFO;
        Map<String,List<?>> map = ExcelFactory.getExcelFactory().getParseExecutor().parse(new File(excelFilePath), JDD.class).get();
        if(map.keySet().size() <= 0){
            System.out.println("没有解析到委托信息表数据！");
            return;
        }
        List<JDD> jddList = (List<JDD>) map.get(map.keySet().iterator().next());
        if(jddList == null){
            System.out.println("没有解析到委托信息表数据！");
            return;
        }
        Random random = new Random();
        Map<String, Map<String,Integer>> tempMap = new HashMap<>();
        List<JDD> errorList = new ArrayList<>();
        for(JDD jdd: jddList){
            String dirName = jdd.getOrderId() + "-" + jdd.getLoanUserName();
            String resourcePath = extractDir + "/" + project.getDebtName() + "/" + dirName;
            String tempTargetDir = targetDir + "/" + project.getDebtName() + "/" + SystemConstant.CASE_FILE_DIR_NAME
                    + "/" + jdd.getLoanUserName() + "-" + jdd.getIdCard();
            try{
                int i = random.nextInt(100);
                File file = new File(resourcePath);
                if(!file.exists()){
                    System.out.println("没有找到文件夹【"+dirName+"】");
                    continue;
                }
                CopyFiles.createDirs(tempTargetDir);
                File[] tempFiles = file.listFiles();
                for(File tempFile : tempFiles){
                    if(tempFile.getName().contains("身份证") || tempFile.getName().contains("活体检测")){
                        System.out.println("-=------------------");
                    }
                    if (tempFile.isFile() && CopyFiles.hasFileNameMark(tempFile.getName(), ignoreFileList)) {
                        continue;
                    }
                    if (tempFile.isDirectory() && CopyFiles.hasFileNameMark(tempFile.getName(), ignoreDirList)) {
                        continue;
                    }
                    String targetRealPath = tempTargetDir + "/" + CopyFiles.getLoanFileEnum(tempFile.getName(),filesMapping).getName();
                    /*if (!CopyFiles.checkHasCopyFileToDirByOnlyOneFileNameMarks(tempFile.getName(), onlyOneFileNameMarks,targetRealPath)) {
                        continue;
                    }*/
                    if(!tempMap.keySet().contains(jdd.getIdCard())){
                        Map<String,Integer> onlyOneFileNameMarks = new HashMap();
                        onlyOneFileNameMarks.put("身份证正面",0);
                        onlyOneFileNameMarks.put("身份证反面",0);
                        onlyOneFileNameMarks.put("活体检测照片",0);
                        tempMap.put(jdd.getIdCard(), onlyOneFileNameMarks);
                    }
                    Map<String,Integer> temp = tempMap.get(jdd.getIdCard());
                    boolean hasContinue = false;
                    for(String key: temp.keySet()){
                        if(tempFile.getName().contains(key)){
                            hasContinue = temp.get(key) == 1;
                            temp.put(key,1);
                            tempMap.put(jdd.getIdCard(),temp);
                            break;
                        }
                    }
                    if(hasContinue){
                        continue;
                    }
                    //CopyFiles.copyFileToDir(tempFile.getAbsolutePath(),targetRealPath );\
                    CopyFiles.copyFileToFile(tempFile.getAbsolutePath(),targetRealPath + "/" +  jdd.getOrderId() + "-" +tempFile.getName());
                    idCardMap.put(jdd.getIdCard(),tempTargetDir);
                }
                //拷贝统一数据
                CopyFiles.copyBaseFiles(tempTargetDir, project);
                System.out.println("整理原始数据成功：【" + dirName + "】");
            }catch (Exception e){
                e.printStackTrace();
                errorList.add(jdd);
            }
        }

        //处理数据u idCardMap : 身份证：地址
        CopyFiles.copyOtherMaterialFileToDir(extractDir,idCardMap);

        if(!errorList.isEmpty()){
            String errorFilePath = targetDir + "/" + project.getDebtName() + "/" + SystemConstant.ERROR_EXCEL_PRE + System.currentTimeMillis() + ".xlsx";
            ExcelFactory.getExcelFactory().getExportExecutor().sheet("整理错误")
                    .data(errorList).head(JDD.class).write(new FileOutputStream(errorFilePath));
            System.out.println("导出整理错误数据【"+errorFilePath+"】");
        }

    }

    public static void main(String[] args) throws Exception {
        ChinaAreaTools.init();
        ProjectUtils.init();
        OrangeMoreMoreCaseFilesFormat orangeMoreMoreCaseFilesFormat = new OrangeMoreMoreCaseFilesFormat();
        String target = "C:\\Users\\liuhj\\Desktop\\案件整理结果";
        String extractDir = "C:\\Users\\liuhj\\Desktop\\桔多多";
        orangeMoreMoreCaseFilesFormat.format(extractDir,target,ProjectUtils.projectMap.get(DetNameEnum.JDD));
    }
}
